/**
 * 学生管理
 */

<template>
  <div class="contenBox">
    <div class="first">
      <el-card class="box-card">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="姓名">
            <el-input v-model="maname" placeholder="请输入学生名称"></el-input>
          </el-form-item>
          <el-form-item label="证件号码">
            <el-input v-model="value" placeholder="请输入证件号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="changeSearch"
              style="
                width: 100px;
                height: 40px;
                background: #7bb4d0;
                border: none;
              "
            >
              <i class="el-icon-search"></i>
              搜索</el-button
            >
            <el-button
              type="success"
              @click="refreshInput"
              style="
                width: 100px;
                height: 40px;
                background: #6ccfcc;
                border: none;
              "
            >
              <i class="el-icon-refresh"></i>
              重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="list">
      <h3 style="color: #3c98d9">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;管理列表
      </h3>
      <div class="btnBox">
        <el-button
          @click="goaddstudent"
          type="primary"
          style="background: #5ca5d2; border: none"
        >
          <i class="el-icon-plus"></i>
          新增</el-button
        >
        <el-upload
          class="upload-demo"
          name="file"
          action="action"
          :show-file-list="false"
          :http-request="fileChange"
        >
          <el-button type="primary" class="daoru">
            <span class="icon iconfont icon-daoru"></span>
            导入学生
          </el-button>
        </el-upload>
        <!-- <el-button
          @click="importCourse"
          type="primary"
          style="background: #66c8e3; border: none"
        >
          <i class="iconfont icon-xiazai" style="width: 25px; height: 25px"></i>
          导入学生</el-button
        > -->
        <el-button
          @click="exportCourse"
          type="primary"
          style="background: #fabd90; border: none"
        >
          <i class="iconfont icon-daochu" style="width: 20px; height: 20px"></i>
          导出名单</el-button
        >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </div>
    </div>
    <div>
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column
            prop="studentName"
            label="姓名"
            itemId="id"
            width="80"
          >
          </el-table-column>
          <el-table-column
            prop="gender"
            :formatter="sfktFormate"
            label="性别"
            width="80"
          >
          </el-table-column>
          <el-table-column prop="identificationTypeName" label="证件类型">
          </el-table-column>
          <el-table-column prop="studentIds" label="学号"> </el-table-column>
          <el-table-column prop="idCardNo" label="证件号码" width="180">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180">
          </el-table-column>
          <el-table-column prop="suspensionStatus" label="停学状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.suspensionStatus"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                :active-value="1"
                :inactive-value="0"
                @change="updateIsUsing(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <span
                class="icon iconfont icon-bianji edit"
                @click="goeditstudent(scope.row)"
              ></span>
              <span
                class="icon iconfont icon-shanchu del"
                @click="del(scope.row)"
              ></span>
              <el-button type="text" @click="Checkstudent(scope.row)"
                >查看详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-size="query.pageSize"
        prev-text="上一页"
        next-text="下一页"
        layout=" prev, pager, next"
        :total="query.total"
      ></el-pagination>

      <div class="jumper">
        跳到第
        <input type="text" ref="pageNum" :value="query.pageNum" />
        页
      </div>
      <button @click="goPage">确定</button>
    </div>
  </div>
</template>

<script>
import {
  exportApi,
  importDataApi,
  delStudentApi,
  editStudentApi,
  getStudentListApi,
} from "@/api/selfStudy/student";

export default {
  data() {
    return {
      options: [],
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      id: "",
      maname: "",
      value: "",
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      current: 1,
      filename: "学生管理表",
    };
  },
  mounted() {
    this.studentlist(this.query);
  },
  methods: {
    sfktFormate(row) {
      if (row.gender == "1") {
        return "女";
      } else if (row.gender == "0") {
        return "男";
      }
    },

    // 获取学生列表
    async studentlist(data) {
      let res = await getStudentListApi(data);
      this.tableData = res.rows;
      this.options = res.rows;
      this.query.total = res.total;
    },

    //搜索
    changeSearch() {
      if (this.maname && this.value) {
        this.studentlist({
          studentName: this.maname.trim(),
          idCardNo: this.value.trim(),
          ...this.query,
        });
      } else if (this.maname && !this.value) {
        this.studentlist({
          studentName: this.maname.trim(),
          ...this.query,
        });
      } else if (!this.maname && this.value) {
        this.studentlist({
          idCardNo: this.value.trim(),
          ...this.query,
        });
      }
    },

    //重置
    refreshInput() {
      this.maname = "";
      this.value = "";
      this.studentlist(this.query);
    },

    // 切换页码
    handleCurrentChange(val) {
      this.query.pageNum = val;
      this.studentlist({
        ...this.query,
        studentName: this.maname.trim(),
        idCardNo: this.value.trim(),
      });
    },

    // 跳转指定页码
    goPage() {
      let { value } = this.$refs.pageNum;
      this.query.pageNum = Number(value);
      this.studentlist({
        ...this.query,
        studentName: this.maname.trim(),
        idCardNo: this.value.trim(),
      });
    },

    // 导入
    fileChange(files) {
      let formData = new FormData();
      formData.append("file", files.file);
      importDataApi(formData).then((res) => {
        if (res.code === 200) {
          this.$message.success("导入成功");
          this.studentlist(this.query);
        }
      });
    },

    //导出
    exportCourse() {
      this.$confirm("是否导出全部数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          exportApi().then((res) => {
            if (!res) return;
            const blob = new Blob([res], {
              type: "application/vnd.ms-excel",
            }); // 构造一个blob对象来处理数据，并设置文件类型
            if (window.navigator.msSaveOrOpenBlob) {
              //兼容IE10
              navigator.msSaveBlob(blob, this.filename);
            } else {
              const href = URL.createObjectURL(blob); //创建新的URL表示指定的blob对象
              const a = document.createElement("a"); //创建a标签
              a.style.display = "none";
              a.href = href; // 指定下载链接
              a.download = this.filename; //指定下载文件名
              a.click(); //触发下载
              URL.revokeObjectURL(a.href); //释放URL对象
            }
          });
        })
        .catch(() => {});
    },

    //跳转
    goaddstudent() {
      this.$router.push("/selfStudy/addstudent");
    },

    // 编辑
    goeditstudent(row) {
      this.$router.push({
        name: "editstudent",
        params: {
          id: row.id,
        },
      });
      localStorage.setItem("id", row.id);
    },

    // 查看详情
    async Checkstudent(row) {
      this.$router.push({
        name: "checkStudent",
        params: {
          id: row.id,
        },
      });
      localStorage.setItem("id", row.id);
    },

    //删除
    del(row) {
      console.log(row);
      this.$confirm("确认删除记录吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          delStudentApi({ ids: JSON.stringify(row.id) }).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功");
              this.studentlist(this.query);
            }
          });
        })
        .catch(() => {});
    },

    //修改状态
    async updateIsUsing(row) {
      let obj = {
        suspensionStatus: row.suspensionStatus ? 1 : 0,
        id: row.id,
      };
      let res = await editStudentApi(obj);
      if (res.code === 200) {
        this.studentlist(this.query);
        this.$message.success("修改成功");
      }
    },
  },
};
</script>
<style lang='scss' scoped >
.contenBox {
  width: 100%;
  height: 100%;
  background-color: #f3f2f7;
  box-sizing: border-box;
  padding: 0 20px 20px;
}
.first {
  width: 100%;
}
.box-card {
  width: 100%;
  .demo-form-inline {
    display: flex;
    justify-content: left;
  }
}
.btnBox {
  display: flex;
  height: 36px;
  ::v-deep .el-button {
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 5px;
  }
  ::v-deep .upload-demo,
  ::v-deep .el-upload {
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.list {
  width: 100%;
  margin-top: 20px;
  padding: 20px 0;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.operation {
  display: flex;
  .el-icon-edit-outline {
    color: #94ccea;
    font-size: 15px;
    margin: 12px 0px;
    font-weight: bold;
  }
  .el-icon-delete {
    color: #f75572;
    font-size: 15px;
    font-weight: bold;
    margin: 12px 8px;
  }
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: right;
  margin-top: 20px;
  margin-right: 40px;
  .jumper {
    font-size: 14px;
    margin-left: 20px;
    margin-right: 10px;
    display: flex;
    align-items: center;
    input {
      width: 40px;
      height: 26px;
      margin: 0 5px;
      text-align: center;
      border: solid 1px #e6e6e6;
      border-radius: 5px;
      color: #666;
      outline-style: none;
    }
  }
  button {
    height: 28px;
    line-height: 28px;
    border-radius: 5px;
    background-color: #4895db;
    border: solid 1px #4895db;
    color: #fff;
    font-size: 14px;
    padding: 0 8px;
  }
}
::v-deep .el-dialog {
  width: 700px;
  height: 500px;
  border-radius: 10px;
  font-weight: 600;
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .message {
    position: absolute;
    left: 40%;
    top: 50%;
    width: 140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    color: #fff;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .fade-enter,
            .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
  .el-dialog__header {
    border-radius: 10px 10px 0 0;
    text-align: center;
    background-color: rgb(72, 149, 219);
    .el-dialog__title {
      color: #fff;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #fff;
    }
  }
  .addInput {
    display: flex;
    justify-content: center;
    align-items: center;
    .collegeName {
      width: 70px;
    }
    .el-input {
      width: 300px;
    }
  }
  .addSelect {
    display: flex;
    justify-content: center;
    align-items: center;
    // justify-content: space-around;

    .superiorCollege {
      width: 70px;
    }
    .el-select {
      width: 300px;
    }
  }
  .el-dialog__body {
    height: 240px;
  }
  .el-dialog__footer {
    text-align: center;
    button {
      width: 100px;
      height: 35px;
      border-radius: 16px;
      line-height: 0;
    }
    .cancel {
      border: none;
      color: #848383;
      background-color: #f0f0f0;
    }
    .determine {
      border: none;
      background-color: #4995dc;
    }
  }
}
</style>
